<?php
  // mail('programming.iter@gmail.com', 'quai ghe ta', 'Chan qua di') or die('no mail()');
  session_start();
  if (!isset($_SESSION["email"])) {
      header('Location: login.php');
  }else{
    if ($_SESSION["email"]!="programming.iter@gmail.com") {
       header('Location: login.php');
    }
  }
?>
<?php include("incs/top.php"); ?>

    <div class="container-fluid">
      <div class="row">
        <?php include("incs/sidebar.php"); ?>
        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main">
          <h1 class="page-header">Hộp thư liên lạc</h1>
          <div class="row placeholders">
              
              <button style="display:none" type="button" id="btn_back" class="btn btn-warning">&nbsp;&nbsp;&nbsp;Quay lại&nbsp;&nbsp;&nbsp;</button>
          
          </div>
          <h3 class="sub-header">Danh sách thư liên lạc</h3>
          <div class="table-responsive">
            <table class="table table-striped">
              <thead>
                <tr>
                  <th>ID</th>
                  <th>Tiêu đề</th>
                  <th>Ngày gửi</th>
                </tr>
              </thead>
              <tbody>
                <?php
                  include("../lib/connection.php");
                  $sql = "SELECT * FROM contact ORDER BY upd_date desc";
                  $query = mysqli_query($conn,$sql);
                  $TRs = "";
                  while ($row = mysqli_fetch_array($query)) {
                    $style="";
                    $id = $row["id"];
                    $email = $row['email'];
                    $title = $row['title'];
                    $content = $row['content'];
                    $upd_date = $row['upd_date'];
                    if (isset($_GET["contact_id"])) {
                       if ($_GET["contact_id"]==$id) {
                          $style ="style='color:red'";
                       }
                    }
                    $TRs .= "<tr><td>$id</td><td><a $style name='contact_$id' href='#'>$email</a> </td><td>$title</td><td>$upd_date</td><td><a name='delete_$id' href='#'>Xóa thư</a> | <a name='sendmail_$email' href='#'>Gửi thư</a></td></tr>";
                  }
                  echo $TRs;
                ?>
             
              </tbody>
            </table>
          </div>

          <?php if (isset($_GET["contact_id"])): ?>
          
          <div class="table-responsive">
            <!--  <form id="fr_member" name="fr_member" action="act/process_save_member.php" method="POST"> -->
            <h3 class="page-header">Chi tiết liên lạc</h3>
            <?php 
              $id = $_GET["contact_id"];
              $sql = "SELECT *
                      FROM contact
                      WHERE id=$id";
              $query = mysqli_query($conn,$sql);
              $row = mysqli_fetch_array($query);

            ?>
  
            <div class="row placeholders">
              <div class="col-sm-2" align="left"><label for="short_content" class="label label-info">Nội dung :</label></div>
              <div class="col-sm-10" align="left">
                <textarea cols="50" rows="5" disabled><?php echo $row["content"]; ?></textarea>
              </div>
            </div>
          <!-- </form> -->
          </div>
        <?php endif;?>
        </div>
      </div>
    </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <!-- Placed at the end of the document so the pages load faster -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
    <script src="../bootstrap/js/bootstrap.min.js"></script>
    <script src="../bootstrap/js/docs.min.js"></script>
    <script src="ckeditor/ckeditor.js"></script>
    <script src="js/common.js"></script>
    <script type="text/javascript">
      var check = <?php echo isset($_GET['member_id'])?0:1 ?>;
      if (check==0) {
        $("#btn_back").show();
      }
      $("a[name^='contact_']").on("click",function(){
        $( this ).css( "color", "red" );
        var name = $(this).attr("name");
        var array = name.split("_");
        var id = array[1];
        window.location.assign("quan-ly-contact.php?contact_id="+id);
      });
      $("#btn_back").on("click",function(){
        window.location.assign("quan-ly-contact.php");
      });
      $("a[name^='delete_']").on("click",function(){
        var name = $(this).attr("name");
        var array = name.split("_");
        var id = array[1];
        window.location.assign("act/process_save_contact.php?contact_id="+id);
      });
      $("a[name^='sendmail_']").on("click",function(){
        var name = $(this).attr("name");
        var array = name.split("_");
        var id = array[1];
        window.location.assign("act/process_send_mail_contact.php?contact_id="+id);
      });
      
    </script>
   
  </body>
</html>